package _struct

import (
	"gitee.com/leeqiang250/go-common/util"
	"math/big"
)

type ERC721TransferEvent struct {
	Contract string `json:"contract"`
	From     string `json:"from"`
	To       string `json:"to"`
	Value    string `json:"value"`
}

func GetERC721TransferEvent(logs []*ETHLog) []*ERC721TransferEvent {
	var err error
	var ok bool
	var value *big.Int
	var log *ETHLog
	var event *ERC721TransferEvent
	var events = make([]*ERC721TransferEvent, 0, len(logs))
	for _, log = range logs {
		if 4 == len(log.Topics) && util.ERC721TransferHash == log.Topics[0] && !log.Removed {
			event = &ERC721TransferEvent{
				Contract: log.Address,
			}
			event.From, err = util.GetETHAddressFromTopic(log.Topics[1])
			if nil != err {
				//todo error
				continue
			}
			event.To, err = util.GetETHAddressFromTopic(log.Topics[2])
			if nil != err {
				//todo error
				continue
			}
			value, ok = new(big.Int).SetString(util.RemoveHexPrefix(log.Topics[3]), 16)
			if !ok {
				//todo error
				continue
			}
			event.Value = value.String()
			events = append(events, event)
		}
	}
	return events
}
